clear all;
clc;

% ------------------------------- Tags ------------------------------------

patnum  = 1;
noaxons = 200;

pol = -1;
if(pol>=0)
    poltag = 'pos_';
else
    poltag = 'neg_';
end

subbp_tag = [poltag,'sub_bp_dc_act_p',num2str(patnum),'_'];
epibp_tag = [poltag,'epi_bp_dc_act_p',num2str(patnum),'_'];


% ------------------------ Import Epidural Data ---------------------------

fval_epi = 0.3;
tval_epi = [0,-10,-20];
nof_epi  = length(fval_epi);
not_epi  = length(tval_epi);
epibp_data  = zeros(noaxons,nof_epi*not_epi);

for ii = 1:nof_epi
    
    s1 = strrep( num2str( fval_epi(ii) ),'.','p' );
 
    for jj = 1:not_epi
        
        kk = not_epi*(ii-1)+jj;
        s2 = strrep( num2str( tval_epi(jj) ),'-','n' );     
        suffix = ['d',s1,'_','t',s2];
        epibp_temp = load([epibp_tag,suffix,'.txt']);
        
        epibp_data(:,kk) = sort(epibp_temp(:,1)*pol);
        
    end
    
end

% ------------------------ Import Subdural Data ---------------------------

fval_sub = [0.2,0.8];
tval_sub = [0,-10,-20];
nof_sub  = length(fval_sub);
not_sub  = length(tval_sub);
subbp_data  = zeros(noaxons,nof_sub*not_sub);

for ii = 1:nof_sub
    
    s1 = strrep( num2str( fval_sub(ii) ),'.','p' );
 
    for jj = 1:not_sub
        
        kk = not_sub*(ii-1)+jj;
        s2 = strrep( num2str( tval_sub(jj) ),'-','n' );     
        suffix = ['d',s1,'_','t',s2];
        subbp_temp = load([subbp_tag,suffix,'.txt']);
        
        subbp_data(:,kk) = sort(subbp_temp(:,1)*pol);
        
    end
    
end

% ------------------------ Processing Data --------------------------------

epibp_min = min(epibp_data,[],2);
epibp_max = max(epibp_data,[],2);

subbp_min = min(subbp_data,[],2);
subbp_max = max(subbp_data,[],2);

% ------------------------ Plotting Data ----------------------------------

fact = 100*(1/noaxons:1/noaxons:1);
fact = fact';

figure;
hold on;
c1 = 0.55;
c2 = 0.1;
fill(cat(1,epibp_min,flipud(epibp_max)),cat(1,fact,flipud(fact)),[c1,c1,c1],'LineWidth',2);
fill(cat(1,subbp_min,flipud(subbp_max)),cat(1,fact,flipud(fact)),[c2,c2,c2],'LineWidth',2);
hold off;
ttl = ['Bipolar Stimulation with AD-TECH Array (Patient ',num2str(patnum),')'];
title(ttl,'FontSize',34,'FontWeight','b');
xlabel('Stimulation Voltage Magnitude (V)','FontSize',30);
ylabel('Percent DC Activated','FontSize',30);
legend('Epidural','Intradural','Location','E');
set(gca,'FontSize',26);

% figure;
% hold on;
% fill(cat(1,submp_min,flipud(submp_max)),cat(1,fact,flipud(fact)),'k','LineWidth',2);
% fill(cat(1,subbp_min,flipud(subbp_max)),cat(1,fact,flipud(fact)),[0.5,0.5,0.5],'LineWidth',2);
% hold off;
% title('Intradural Stimulation','FontSize',36,'FontWeight','b');
% xlabel('Cathodic Stimulation Voltage (V)','FontSize',30);
% ylabel('DC Fibers Activated (%)','FontSize',30);
% legend('Monopolar','Bipolar','Location','E');
% set(gca,'FontSize',26);